package sort;

import java.util.Arrays;

/**
 * 希尔排序
 */
public class ShellSort {
    public static void main(String[] args) {
        int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void shellSort(int[] arr) {
        //希尔排序方法
        int len = arr.length;
        //希尔排序 每次排序都要把组数缩小一半
        for (int gap = len / 2; gap > 0; gap /= 2) {
            //接下来的代码可以理解为插入排序
            for (int i = gap; i < len; i++) {
                int insertValue = arr[i];
                int j = i - gap;
                for (; j >= 0; j -= gap) {
                    if (insertValue < arr[j]) {
                        arr[j+gap] = arr[j];
                    } else {
                        break;
                    }
                }
                arr[j+ gap] = insertValue;
            }

        }
    }
}
